package com.faner.fast.upms.domain.bo;

import com.faner.fast.base.TreeNode;
import com.faner.fast.upms.domain.vo.MenuVO;
import com.faner.fast.upms.model.SysMenuDO;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @作者 faner
 * @创建时间 2021/1/26 12:27
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class MenuTree extends TreeNode implements Serializable {

    /**
     * 菜单图标
     */
    private String icon;

    /**
     * 菜单名称
     */
    private String name;

    private boolean spread = false;

    /**
     * 前端路由标识路径
     */
    private String path;

    /**
     * 路由缓冲
     */
    private String keepAlive;

    /**
     * 权限编码
     */
    private String permission;

    /**
     * 菜单类型 （0菜单 1按钮）
     */
    private String type;

    /**
     * 菜单标签
     */
    private String label;

    /**
     * 排序值
     */
    private Integer sort;

    /**
     * 是否包含子节点
     *
     * @since 3.7
     */
    private Boolean hasChildren;

    public MenuTree() {
    }

    public MenuTree(int id, String name, int parentId) {
        this.id = id;
        this.parentId = parentId;
        this.name = name;
        this.label = name;
    }

    public MenuTree(int id, String name, MenuTree parent) {
        this.id = id;
        this.parentId = parent.getId();
        this.name = name;
        this.label = name;
    }

    public MenuTree(MenuVO menuVo) {
        this.id = menuVo.getId();
        this.parentId = menuVo.getParentId();
        this.icon = menuVo.getIcon();
        this.name = menuVo.getName();
        this.path = menuVo.getPath();
        this.type = menuVo.getType();
        this.permission = menuVo.getPermission();
        this.label = menuVo.getName();
        this.sort = menuVo.getSort();
        this.keepAlive = menuVo.getKeepAlive();
        this.hasChildren = false;
    }

    /**
     * 通过sysMenu创建树形节点
     * @param menus
     * @param root
     * @return
     */
    public List<MenuTree> buildTree(List<SysMenuDO> menus, long root) {
        List<MenuTree> trees = new ArrayList<>();
        MenuTree node;
        for (SysMenuDO menu : menus) {
            node = new MenuTree();
            node.setId(menu.getId());
            node.setParentId(menu.getParentId());
            node.setName(menu.getName());
            node.setPath(menu.getPath());
            node.setPermission(menu.getPermission());
            node.setLabel(menu.getName());
            node.setIcon(menu.getIcon());
            node.setType(menu.getType());
            node.setSort(menu.getSort());
            node.setHasChildren(false);
            node.setKeepAlive(menu.getKeepAlive());
            trees.add(node);
        }
        return com.faner.fast.util.TreeUtil.build(trees, root);
    }
}

